package cn.app.edu.projectpracticeofpracticaltraining;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Objects;

import cn.app.edu.projectpracticeofpracticaltraining.fragment.HomeFragment;
import cn.app.edu.projectpracticeofpracticaltraining.fragment.MineFragment;
import cn.app.edu.projectpracticeofpracticaltraining.fragment.ProjectFragment;
import cn.app.edu.projectpracticeofpracticaltraining.fragment.PublicFragment;
import cn.app.edu.projectpracticeofpracticaltraining.fragment.SystemFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout mHome;
    private LinearLayout mSystem;
    private LinearLayout mPublic;
    private LinearLayout mProject;
    private LinearLayout mMine;
    private ViewPager mViewPage;
    private TextView mTitle;
    private ArrayList<Fragment> mFragmentList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Objects.requireNonNull(getSupportActionBar()).hide();


        findViews();
        initData();
        initEvent();

    }

    private void findViews() {
        // 条目点击
        mHome = findViewById(R.id.ly_home_activity_main);
        mSystem = findViewById(R.id.ly_system_activity_main);
        mPublic = findViewById(R.id.ly_public_activity_main);
        mProject = findViewById(R.id.ly_project_activity_main);
        mMine = findViewById(R.id.ly_mine_activity_main);
        // viewpager
        mViewPage = findViewById(R.id.vp_page_activity_main);
        // 标题
        mTitle = findViewById(R.id.tv_title_activity_main);
    }

    private void initData() {
        mHome.setOnClickListener(this);
        mSystem.setOnClickListener(this);
        mPublic.setOnClickListener(this);
        mProject.setOnClickListener(this);
        mMine.setOnClickListener(this);

        mFragmentList.add(HomeFragment.newInstance());
        mFragmentList.add(SystemFragment.newInstance());
        mFragmentList.add(MineFragment.newInstance());
        mFragmentList.add(ProjectFragment.newInstance());
        mFragmentList.add(PublicFragment.newInstance());
    }

    private void initEvent() {
        MyViewPageAdapter myViewPageAdapter = new MyViewPageAdapter(getSupportFragmentManager());
        mViewPage.setAdapter(myViewPageAdapter);


        mViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        setTitle("首页");

                        break;
                    case 1:
                        setTitle("知识体系");

                        break;
                    case 2:
                        setTitle("公众号");

                        break;
                    case 3:
                        setTitle("项目");

                        break;
                    case 4:
                        setTitle("我的");

                        break;
                    default:
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.ly_home_activity_main:
                mViewPage.setCurrentItem(0);
                break;
            case R.id.ly_system_activity_main:
                mViewPage.setCurrentItem(1);
                break;
            case R.id.ly_public_activity_main:
                mViewPage.setCurrentItem(2);
                break;
            case R.id.ly_project_activity_main:
                mViewPage.setCurrentItem(3);
                break;
            case R.id.ly_mine_activity_main:
                mViewPage.setCurrentItem(4);
                break;
            default:
                break;
        }
    }

    public void setTitle(String title){
      mTitle.setText(title);
    }

    class MyViewPageAdapter extends FragmentStatePagerAdapter {
        public MyViewPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return mFragmentList.get(i);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }
    }


}